package cappuccino.couldbook.ui;

import android.content.Intent;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cappuccino.couldbook.Constants;
import cappuccino.couldbook.R;
import cappuccino.couldbook.base.BaseActivity;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.data.User;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/5.
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener{

    String TAG = "RegisterActivity";

    @Override
    protected void init() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        showBack();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    TextInputLayout etUsername;
    TextInputLayout etLoginname;
    TextInputLayout etLoginpass;
    Button btSubmit;
    @Override
    protected void bindView() {
        setContentView(R.layout.activity_register);
        etUsername = (TextInputLayout)findViewById(R.id.til_register_username);
        etLoginname = (TextInputLayout)findViewById(R.id.til_register_loginname);
        etLoginpass = (TextInputLayout)findViewById(R.id.til_register_loginpass);
        btSubmit = (Button)findViewById(R.id.bt_register_submit);
        btSubmit.setOnClickListener(this);
        setResult(0,null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_register_submit:
                register();
                break;
            default:
                break;
        }
    }

    Observer<Result<User>> observer = new Observer<Result<User>>() {

        @Override
        public void onCompleted() {
            Log.v(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable throwable) {
            Log.v(TAG, "onError");
            Toast.makeText(RegisterActivity.this.getApplicationContext(), "We meet some problem ", Toast.LENGTH_SHORT).show();
            Log.v(TAG,throwable.toString());
        }

        @Override
        public void onNext(Result<User> res) {
            Log.v(TAG, "OnNext");
            if (res.status == 1) {
                Toast.makeText(RegisterActivity.this.getApplicationContext(), "Register success " + etUsername.getEditText().getText().toString(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("username",etLoginname.getEditText().getText().toString());
                intent.putExtra("userpass",etLoginpass.getEditText().getText().toString());
                setResult(Constants.REGISTER_SUCCESS,intent);
                finish();
            }
            else
                Toast.makeText(RegisterActivity.this.getApplicationContext(),"Fail:"+res.message,Toast.LENGTH_SHORT).show();

            unlockEt();
        }
    };

    private void lockEt(){
        etUsername.setFocusable(false);
        etLoginname.setFocusable(false);
        etLoginpass.setFocusable(false);
    }

    private void unlockEt(){
        etUsername.setFocusable(true);
        etLoginname.setFocusable(true);
        etLoginpass.setFocusable(true);
    }


    private void register(){
        lockEt();
        Log.v(TAG,"clicked");
        NetApi api = Network.getNetApi();
        String username = etUsername.getEditText().getText().toString();
        String loginanme = etLoginname.getEditText().getText().toString();
        String loginpass = etLoginpass.getEditText().getText().toString();
        String tip = "";
        if(username == null || username.isEmpty())
            tip+="用户名";
        if(loginanme == null || loginanme.isEmpty()) {
            if(!tip.isEmpty())
                tip+="、";
            tip += "登录名";
        }
        if(loginpass == null || loginpass.isEmpty()){
            if(!tip.isEmpty())
                tip+="、";
            tip+="登录密码";
        }
        if(!tip.isEmpty())
           Toast.makeText(this.getApplicationContext(),tip+"不能为空",Toast.LENGTH_SHORT).show();
        else{
            api.register(username,loginanme,loginpass).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        }
    }

}
